﻿using System.Web.UI;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Web.Caching;
using Liber.WEB;
using Liber;

namespace SoBusiness.Controllers
{
    public class PurchaseController :LiberController
    {
        [AppAurhority("Purchase","Order","Admin")]
        public ViewResult Orders()
        {
            return View();
        }

        public ViewResult MyOrders()
        {
            return View();
        }

        public ViewResult Inquriy()
        {
            return View();
        }

        [AppAurhority("Purchase", "Receipt", "Query")]
        public ViewResult Receipt()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        [WxSession]
        [NoSession]
        public ViewResult WxMyOrders()
        {
            return View();
        }

        public ViewResult WechatReceipt(long oid = -1)
        {
            return View();
        }

        //public JsonResult GetOrder(long oid = -1)
        //{
        //    return Json(API.Request("PurchaseApp@GetOrderDetail")
        //                   .Set("orderid", oid)
        //                   .WaitResult());
        //}


        public JsonResult QueryOrders()
        {
            bool onlymine = Request.QueryString.TryGetBoolean("onlymine", false);
            string status = Request.QueryString.TryGet("status", "all");
            string flag = Request.QueryString.TryGet("flag", "");
            string ordDate = Request.QueryString.TryGet("ordDate", "all");
            string dlvDate = Request.QueryString.TryGet("dlvDate", "all");
            string ordDateStart = Request.QueryString.TryGet("orderDateStart", "");
            string ordDateEnd = Request.QueryString.TryGet("orderDateEnd", "");
            string dlvDateStart = Request.QueryString.TryGet("dlvDateStart", "");
            string dlvDateEnd = Request.QueryString.TryGet("dlvDateEnd", "");
            string supplier = Request.QueryString.TryGet("supplier", "");
            string buyer = Request.QueryString.TryGet("buyer", "");
            string constructor = Request.QueryString.TryGet("useunit", "");

            long org = Request.QueryString.TryGetLong("org", -1);
            int page = Request.QueryString.TryGetInt("page", -1);
            int num = Request.QueryString.TryGetInt("num", 0);
            string desc = Request.QueryString.TryGet("desc", "");
            string asc = Request.QueryString.TryGet("asc", "");
            var rtn = API.Request("PurchaseApp@QueryOrder")
                         .Set("onlymine", onlymine)
                         .Set("status", status)
                         .Set("flag", flag)
                         .Set("ordDate", ordDate)
                         .Set("dlvDate", dlvDate)
                         .Set("ordDateStart", ordDateStart)
                         .Set("ordDateEnd", ordDateEnd)
                         .Set("dlvDateStart", dlvDateStart)
                         .Set("dlvDateEnd", dlvDateEnd)
                         .Set("supplier", supplier)
                         .Set("constructor",constructor)
                         .Set("buyer", buyer)
                         .Set("org", org)
                         .Set("page", page)
                         .Set("num", num)
                         .Set("desc", desc)
                         .Set("asc", asc)
                         .WaitResult();
            return Json(rtn);
        }

        [AppAurhority("Purchase", "Receipt", "Create")]
        public JsonResult ReceiptDelivery(string rcptMX = "")
        {
            var mx = XMLSetting.LoadFromStr(rcptMX);
            var its = mx.All("item");
            List<DyObject> itms = new List<DyObject>();
            for (var i = 0; i < its.Length; i++)
            {
                var itm = new DyObject();
                itm.Set("dlvmxidstr", its[i].First("dlvmxidstr").Value)
                   .Set("receipt", its[i].First("receipt").Value)
                   .Set("remark", its[i].First("remark").Value)
                   .Set("ordermxidstr", its[i].First("ordermxidstr").Value);
                itms.Add(itm);
            }
            DyObject rcptData = new DyObject().Set("dlvid", mx.First("dlvid").Value.ToString())
                                              .Set("ordid",mx.First("orderid").Value.ToString())
                                              .Set("items", itms);
            var rsult = API.Request("PurchaseApp@NewReceipt")
                         .Set("receiptdata", rcptData)
                         .WaitResult();
            if (rsult.IsPositive)
            {
                TLB2BController.CheckMessage();
            }
            return Json(rsult);

        }

        /// <summary>
        /// 获取订单各种执行进度的统计结果
        /// </summary>
        /// <returns></returns>
        public JsonResult CountOrders()
        {
            bool onlymine = Request.QueryString.TryGetBoolean("onlymine", false);
            string status = Request.QueryString.TryGet("status", "all");
            string flag = Request.QueryString.TryGet("flag", "");
            string ordDate = Request.QueryString.TryGet("ordDate", "all");
            string dlvDate = Request.QueryString.TryGet("dlvDate", "all");
            string ordDateStart = Request.QueryString.TryGet("orderDateStart", "");
            string ordDateEnd = Request.QueryString.TryGet("orderDateEnd", "");
            string dlvDateStart = Request.QueryString.TryGet("dlvDateStart", "");
            string dlvDateEnd = Request.QueryString.TryGet("dlvDateEnd", "");
            string supplier = Request.QueryString.TryGet("supplier", "");
            string saler = Request.QueryString.TryGet("saler", "");

            long org = Request.QueryString.TryGetLong("org", -1);
            var rtn = API.Request("PurchaseApp@QueryOrder")
                         .Set("onlymine", onlymine)
                         .Set("onlycount",true)
                         .Set("status", status)
                         .Set("flag", flag)
                         .Set("ordDate", ordDate)
                         .Set("dlvDate", dlvDate)
                         .Set("ordDateStart", ordDateStart)
                         .Set("ordDateEnd", ordDateEnd)
                         .Set("dlvDateStart", dlvDateStart)
                         .Set("dlvDateEnd", dlvDateEnd)
                         .Set("supplier", supplier)
                         .Set("saler", saler)
                         .Set("org", org)
                         .WaitResult();
            return Json(rtn);
        }

        public JsonResult GetTodo(long orderid = -1)
        {
            return Json(API.Request("TodoSvc@GetTODO")
                           .Set("objid", orderid)
                           .WaitResult());
        }

        [AppAurhority("Purchase", "Receipt", "Query")]
        public JsonResult QueryReceipts(string status = "", string flag = "", string dlvDate = "all")
        {
            var rtn = API.Request("PurchaseApp@QueryReceipts")
                         .Set("status", status)
                         .Set("flag", flag)
                         .Set("dlvDate", dlvDate)
                         .WaitResult();
            return Json(rtn);
        }
    }
}